﻿define(['esri/layers/GraphicsLayer', 'esri/PopupTemplate', 'esri/Graphic'], function (
    GraphicsLayer,
    PopupTemplate,
    Graphic
) {
    var PolygonLayer = GraphicsLayer.createSubclass({
        setData: function (viewUtils, layer) {
            var me = this;
            if (!viewUtils) {
                return;
            }
            me.viewUtils = viewUtils;
            var options = layer.layer || {};
            var data = options.data || [];
            if (data.length < 1) {
                if (typeof options.callback == 'function') {
                    options.callback(layer);
                }
            }
            var popupOptions = options.popupOptions;
            var popupTemplateOptions = options.popupTemplateOptions;
            var symbolFieldName = options.symbolFieldName || 'symbol';
            var pathsFieldName = options.ringsFieldName || 'paths';
            var sr = options.sr;
            let symbol = {
                type: 'simple-line', // autocasts as new SimpleLineSymbol()
                color: 'lightblue',
                width: '2px',
                style: 'short-dot',
            };
            data.map((item, index) => {
                const polyline = {
                    type: 'polyline',
                    paths: [],
                };
                if (pathsFieldName) {
                    polyline.paths = item[pathsFieldName];
                }
                if (item[symbolFieldName]) {
                    me.addGraphic(polyline, item[symbolFieldName], item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                } else {
                    me.addGraphic(polyline, symbol, item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                }
            });
        },
        addGraphic: function (geometry, symbol, item, popupTemplateOptions, sr) {
            var me = this;

            var popupTemplate = null;
            if (popupTemplateOptions) {
                popupTemplate = new PopupTemplate();
                popupTemplate.title = popupTemplateOptions.getTitle(item);
                popupTemplate.content = popupTemplateOptions.getContent(item);
            }

            var graphic = this.createGraphic({
                geometry: geometry,
                symbol: symbol,
                attributes: item,
                popupTemplate: popupTemplate,
            });
            me.add(graphic);
        },
        getGraphicLocation: function (viewUtils, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!viewUtils) {
                return;
            }

            var map = viewUtils.getMap();
            if (!map) {
                return;
            }

            var geometry = esriEvent.mapPoint || {};

            var lngFieldName = 'x';
            var latFieldName = 'y';
            var location = {};
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        },
        createGraphic(t) {
            t = Object.assign(
                { geometry: null, symbol: null, attributes: null, popupTemplate: null },
                t
            );
            let g = new Graphic(t);
            return g;
        },
    });
    return PolygonLayer;
});
